package frsf.cidisi.exercise.tp1_ia.search;

import frsf.cidisi.faia.agent.Action;
import frsf.cidisi.faia.environment.Environment;

public class TKComputers extends Environment {

    public TKComputers() {
        // Create the environment state
        this.environmentState = new EstadoAmbiente();
    }

    public EstadoAmbiente getEnvironmentState() {
        return (EstadoAmbiente) super.getEnvironmentState();
    }

    /**
     * This method is called by the simulator. Given the Agent, it creates
     * a new perception reading, for example, the agent position.
     * @param agent
     * @return A perception that will be given to the agent by the simulator.
     */
    @Override
    public  ComputersMarketPerception getPercept() {
    	// Create a new perception to return
        ComputersMarketPerception perception = new ComputersMarketPerception();
        EstadoAmbiente environmentState = this.getEnvironmentState();       
        int posicionAgente = environmentState.getposicionAgente();
        if (environmentState.gettpoTranscurrido()>=168){
        	perception.setcancel_demandforecast(ComputersMarketPerception.CANCEL_DEMAND_FORECAST);
        	perception.setStr("Cancel Demand Forecast");
        }
        if (posicionAgente == EstadoAgente.INITIAL){
        	perception.setwaiting_request(ComputersMarketPerception.WAITING_REQUEST);
        	perception.setStr("Waiting for Request");
        }
        else if (posicionAgente == EstadoAgente.FORECAST_REQUEST_SENT){
        	/*
        	 * debe aceptarse o rechazarse la propuesta 
        	 */
        	int centinela = ComputersMarketPerception.aleatorio(0,1);
        	
        	if(centinela == 0){
        		perception.setrefuse_forecastrequestresponse(ComputersMarketPerception.REFUSE_FORECAST_REQUEST);
       		 	perception.setStr("Refuse Forecast Request");
       	 	}
       	 	else {
       	 		perception.setagree_forecastrequestresponse(ComputersMarketPerception.AGREE_FORECAST_REQUEST);
       	 		perception.setStr("Agree Forecast Request");
       	 	}
        }
        else if (posicionAgente == EstadoAgente.INFORM_PLANNED_EVENTS_SENT){
        		perception.setpropose_demandforecast1(ComputersMarketPerception.PROPOSE_DEMAND_FORECAST_1);
        		perception.setStr("Propose Demand Forecast1");
        }
        else if (posicionAgente == EstadoAgente.INFORM_POS_FORECAST_SENT){
        		perception.setpropose_demandforecast1(ComputersMarketPerception.PROPOSE_DEMAND_FORECAST_1);
        		perception.setStr("Propose Demand Forecast1");
        }
        
        /*
         * final del subproceso 3
         */
        else if (posicionAgente == EstadoAgente.ACCEPT_PROPOSAL_SENT){
        		
           	 	perception.setpropose_demandforecast2(ComputersMarketPerception.PROPOSE_DEMAND_FORECAST_2);
           	 	perception.setStr("Propose Demand Forecast2");   	 	
        	
        }
        
        /*
         * fin subproceso 3
         */
        else if (posicionAgente == EstadoAgente.UPDATED_DEMAND_FORECAST_SENT){
        	
       	 	perception.setpropose_demandforecast1(ComputersMarketPerception.PROPOSE_DEMAND_FORECAST_1);
       	 	perception.setStr("Propose Demand Forecast1");
       	 	
        }
        else if (posicionAgente == EstadoAgente.SUBPROCESS_3_FINALIZED){
    		perception.setinform_issues(ComputersMarketPerception.INFORM_ISSUES);
    		perception.setStr("Inform Issues");
        }
        
       // Return the perception
       return perception;
    }

    
    public String toString() {
        return environmentState.toString();
    }

    
    public boolean agentFailed(Action actionReturned) {

        EstadoAmbiente envState =
                this.getEnvironmentState();
        if(envState == null){
        	System.out.println("ES NULL");
        }
        if (envState != null &&
        		envState.getposicionAgente()== EstadoAgente.FORECAST_REQUEST_REFUSE)
        	return true;
        
        
        return false;
    }   
}
